<template>
  <div style="height: 100%">
    <div class="adapt">
      <div class="wapper">
        <div class="cartadapt" v-for="(item, index) in skuList" :key="index">
          <!-- 滑动单元格用于删除操作 -->
          <van-swipe-cell class="swopeCell">
            <div class="leftCont">
              <!--  -->
              <input
                type="checkbox"
                v-model="getGoodsSelected[item.id]"
                class="checkbox"
                @change="selectedChange(item.id, getGoodsSelected[item.id])"
              />
              <!--图片 -->
              <van-card
                class="cartinfo"
                :desc="item.description"
                :title="item.title"
                :thumb="item.img"
              >
                <!-- 类型 -->
                <template #tags>
                  <van-tag
                    class="cartinfo-tag"
                    plain
                    v-for="(item2, indext2) in item.specs"
                    :key="indext2"
                    >{{ item2.value }}</van-tag
                  >
                </template>
                <!-- 价格 -->
                <template #bottom>
                  <div class="cart-price">￥{{ item.price }}</div>
                  <!-- 个数 -->
                  <van-stepper
                    v-model="getGoodsCount[item.id]"
                    @change="countChanged(item.id, getGoodsCount[item.id])"
                    class="cart-stepper"
                  />
                </template>
              </van-card>
            </div>
            <!-- 删除 -->
            <template #right>
              <van-button
                @click="removeCarItem(item.id)"
                square
                text="删除"
                type="danger"
                class="delete-button"
              ></van-button>
            </template>
          </van-swipe-cell>
        </div>
      </div>
      <!-- 购物车底部操作栏 -->
      <div class="account">
        <van-button class="pay" @click="settlementClick">结算</van-button>
        <div>
          <div class="cart-choice">
            全选<input
              type="checkbox"
              :checked="pdquanxuan()"
              @click="getchecked"
            />
          </div>
          <div class="cart-choice">
            已选:<span class="highlight">{{ getSelectedCount }}</span
            >件
          </div>
          <div class="cart-sum">
            合计:<span class="highlight">{{ getSelectedAmount }}</span>
          </div>
        </div>
      </div>
    </div>
    <NavBarVue></NavBarVue>
  </div>
</template>

<script>
import { mapGetters, mapState, mapMutations } from "vuex";
import NavBarVue from "@/components/NavBar.vue";

export default {
  name: "Shopcart",
  data() {
    return {
      goodsList: [],
      skuList: [],
    };
  },
  components: {
    NavBarVue,
  },
  created() {
    this.getSkuDetail();
  },
  computed: {
    ...mapState("shopcart", ["car"]),
    ...mapGetters("shopcart", [
      "getGoodsCount",
      "getGoodsSelected",
      "getSelectedCount",
      "getSelectedGoods",
    ]),
    ...mapMutations("shopcart", ["updateGoodsSelected"]),
    ...mapGetters(["addOrder"]),
    getSelectedAmount() {
      if (this.skuList) {
        var goods = this.getSelectedGoods;
        var amount = 0;
        this.skuList.forEach((item) => {
          if (goods[item.id]) {
            amount += item.price * goods[item.id].buyAmount;
          }
        });
        return amount.toFixed(2);
      } else {
        return "";
      }
    },
  },
  methods: {
    // 商品数量的改变调用
    countChanged(id, count) {
      this.$store.commit("shopcart/updateGoodsInfo", {
        id: id,
        buyAmount: count,
      });
    },
    // 删除购物车里某一样商品
    removeCarItem(id) {
      this.$store.commit("shopcart/removeCar", id);
      this.getSkuDetail();
    },
    // 请求sku的详情信息
    getSkuDetail() {
      let list = this.car.map((item) => item.skuId);
      let params = {
        ids: list.join(),
      };
      console.log("params:", params);
      this.$http
        .get("http://localhost:8080/sku/skus", {
          params: params,
        })
        .then((res) => {
          this.skuList = res.data;
          console.log(this.skuList);
        });
    },
    // 改变商品的选择状态调用vuex中的updateGoodsSelected
    selectedChange(id, val) {
      this.$store.commit("shopcart/updateGoodsSelected", {
        id: id,
        selected: val,
      });
    },
    // 点击结算
    settlementClick() {
      if (this.skuList) {
        var goods = this.getSelectedGoods;
        this.skuList.forEach((item) => {
          if (item.id == goods[item.id].skuId) {
            item.buyAmount = goods[item.id].buyAmount;
          }
        });
        this.$store.commit("addOrder", this.skuList);
        this.$router.push({
          name: "buy",
          params: {
            buyMode: 1,
          },
        });
      }
    },
    // 判断是否全选
    pdquanxuan() {
      if (this.skuList) {
        let pp = true;
        this.skuList.forEach((item) => {
          // console.log("this.getGoodsSelected[item.id]",this.getGoodsSelected[item.id]);
          if (this.getGoodsSelected[item.id] == false) {
            pp = false;
          }
        });
        if (pp) {
          return true;
        } else {
          return false;
        }
      }
    },
    // 使列表全选或全取消
    getchecked(event) {
      // console.log("event：",event.target.checked);
      this.$store.commit("shopcart/updatapd", event.target.checked);
    },
  },
};
</script>

<style scoped>
.adapt {
  overflow: hidden;
  zoom: 1;
}
.wapper {
  overflow-y: auto;
  height: calc(100vh - 50px - 50px - 46px);
}
.checkbox {
  float: left;
  height: 120px;
  width: 10px;
  margin: 0 10px;
}
.account {
  position: fixed;
  bottom: 50px;
  height: 50px;
  width: 100%;
  background: rgb(240, 237, 237);
}
.cart-choice {
  float: left;
  line-height: 50px;
  padding-left: 20px;
}
.cart-sum {
  float: right;
  line-height: 50px;
  padding-right: 10px;
}
.pay {
  float: right;
  height: 40px;
  width: 100px;
  color: white;
  background: rgb(105, 134, 140);
  margin-top: 5px;
  margin-right: 5px;
  border-radius: 8px;
  border: none;
}
.delete-button {
  height: 100%;
}
.leftCont {
  border-bottom: 1px solid #eee;
  display: flex;
  height: 100%;
}
.highlight {
  color: rgb(207, 175, 116);
  padding-right: 5px;
}
.cart {
  text-align: left;
  width: 100%;
}
.cartinfo {
  flex: 1;
  float: left;
  font-size: 13px;
  margin: 0;
  background-color: #fff;
}
.cart-price {
  float: left;
  font-size: 18px;
  line-height: 30px;
}
.cart-stepper {
  float: right;
}
.cartinfo-tag {
  margin: 10px 0;
  margin-right: 5px;
}
</style>
